from strategy.StrategyTemplate import StrategyTemplate
from utils.jsl_data_map import default_columns
from strategy import strategy_filter
from strategy.base_strategy.base_strategy_common import *
from utils.db import DbHandler

class BreakEvenRank(StrategyTemplate):
    """
    保本策略:
        低于到期赎回

    """
    def __init__(self):
        super(BreakEvenRank, self).__init__()
        self.strategy_name = "保本(低于到期赎回)策略"
        self.strategy_descrption = "现价低于到期赎回价，按照现价与到期赎回价差值从大到小排序"

    def run(self):
        # 基础过滤，过滤掉新债、强赎、EB
        self.data = strategy_filter.filter_base(self.data)
        # 筛选标准
        self.data = self.data[self.data['现价'] < self.data['到期赎回价']]
        self.data['到期赎回差'] = round(self.data['到期赎回价'] - self.data['现价'], 1)
        # sort
        self.data = self.data .sort_values(by='到期赎回差', ascending=False)
        default_columns.extend(['到期赎回价', '到期赎回差'])
        # 获得默认列
        self.data = self.data[default_columns]
        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)

    def api(self, size=10):
        db = DbHandler()
        db.create_session()
        super(self.__class__, self).api(db=db)
        # 筛选标准
        self.data = self.data[self.data['price'] < self.data['redeem_price_total']]
        self.data['redeem_price_total_diff'] = round(self.data['redeem_price_total'] - self.data['price'], 1)
        # sort
        self.data = self.data.sort_values(by='redeem_price_total_diff', ascending=False)        # 获得默认列
        self.data = self.data[breakeven_column][:size]

        # 清掉原来的索引
        self.data.reset_index(inplace=True, drop=True)
        print(self.data)
        self.db.close_session()
        return self.data

if __name__ == '__main__':
    ins = BreakEvenRank()
    ins.topN = 20
    ins.test()

